package fr.LesLoutresSauvages.laloutresportive.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import fr.LesLoutresSauvages.laloutresportive.R;
import fr.LesLoutresSauvages.laloutresportive.util.Live;
import fr.LesLoutresSauvages.laloutresportive.util.LiveCreation;
import fr.LesLoutresSauvages.laloutresportive.util.RequetesHttp;

public class CreerLiveActivity extends Activity implements OnClickListener
{
  // ** Declaration des variables pour manipuler les objets graphiques ** //
  private Button creerBtValider;
  private EditText creerEdtTxtCommentateur;
  private EditText creerEdtTxtCompetitionId;
  private EditText creerEdtTxtDebut;
  private EditText creerEdtTxtDepartementId;
  private EditText creerEdtTxtEquipe1;
  private EditText creerEdtTxtEquipe2;
  private EditText creerEdtTxtLatitude;
  private EditText creerEdtTxtLongDescription;
  private EditText creerEdtTxtLongitude;
  private EditText creerEdtTxtNom;
  private EditText creerEdtTxtShortDescription;
  private EditText creerEdtTxtSportId;

  @Override
  protected void onCreate(Bundle savedInstanceState)
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_creer_live);

    // ** Mapping des variables pour manipuler les objets graphiques ** //
    this.creerBtValider = (Button) findViewById(R.id.creerBtValider);
    this.creerEdtTxtCommentateur = (EditText) findViewById(R.id.creerEdtTxtCommentateur);
    this.creerEdtTxtCompetitionId = (EditText) findViewById(R.id.creerEdtTxtCompetitionId);
    this.creerEdtTxtDebut = (EditText) findViewById(R.id.creerEdtTxtDebut);
    this.creerEdtTxtDepartementId = (EditText) findViewById(R.id.creerEdtTxtDepartementId);
    this.creerEdtTxtEquipe1 = (EditText) findViewById(R.id.creerEdtTxtEquipe1);
    this.creerEdtTxtEquipe2 = (EditText) findViewById(R.id.creerEdtTxtEquipe2);
    this.creerEdtTxtLatitude = (EditText) findViewById(R.id.creerEdtTxtLatitude);
    this.creerEdtTxtLongDescription = (EditText) findViewById(R.id.creerEdtTxtLongDescription);
    this.creerEdtTxtLongitude = (EditText) findViewById(R.id.creerEdtTxtLongitude);
    this.creerEdtTxtNom = (EditText) findViewById(R.id.creerEdtTxtNom);
    this.creerEdtTxtShortDescription = (EditText) findViewById(R.id.creerEdtTxtShortDescription);
    this.creerEdtTxtSportId = (EditText) findViewById(R.id.creerEdtTxtSportId);

    // ** Ajout des ecouteurs d'evenements sur les objets graphiques ** //
    this.creerBtValider.setOnClickListener(this);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu)
  {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_creer_live, menu);
    return true;
  }

  @Override
  public void onClick(View v)
  {
    if (v == this.creerBtValider)
    {
      // ** Décrypter les EditBox pour envoyer les infos à la fonction qui
      // transforme en JSON ** //
      LiveCreation liveCree = new LiveCreation();

      String edtTxtNomCommentateur = this.creerEdtTxtCommentateur.getText().toString();
      int edtTxtCompetitionId = Integer.parseInt(this.creerEdtTxtCompetitionId.getText().toString());
      String edtTxtDebut = this.creerEdtTxtDebut.getText().toString();
      int edtTxtDepartementId = Integer.parseInt(this.creerEdtTxtDepartementId.getText().toString());
      String edtTxtEquipe1 = this.creerEdtTxtEquipe1.getText().toString();
      String edtTxtEquipe2 = this.creerEdtTxtEquipe2.getText().toString();
      String edtTxtLatitude = this.creerEdtTxtLatitude.getText().toString();
      String edtTxtLongitude = this.creerEdtTxtLongitude.getText().toString();
      String edtTxtLongDescription = this.creerEdtTxtLongDescription.getText().toString();
      String edtTxtShortDescription = this.creerEdtTxtShortDescription.getText().toString();
      String edtTxtNom = this.creerEdtTxtNom.getText().toString();
      int edtTxtSportId = Integer.parseInt(this.creerEdtTxtSportId.getText().toString());

      liveCree.setNomCommentateur(edtTxtNomCommentateur);
      liveCree.setCompetitionId(edtTxtCompetitionId);
      liveCree.setDebut(edtTxtDebut);
      liveCree.setDepartementId(edtTxtDepartementId);
      liveCree.setEquipe1(edtTxtEquipe1);
      liveCree.setEquipe2(edtTxtEquipe2);
      liveCree.setLatitude(edtTxtLatitude);
      liveCree.setLongitude(edtTxtLongitude);
      liveCree.setLongDescription(edtTxtLongDescription);
      liveCree.setShortDescription(edtTxtShortDescription);
      liveCree.setNom(edtTxtNom);
      liveCree.setSportId(edtTxtSportId);

      // ** Envoyer le flux JSON sur HTTP ** //
      RequetesHttp rq = new RequetesHttp();
      Live liveRecu = new Live();
      String liveRecuJson = rq.postCreationLive(liveCree);

      // ** Recevoir le live cree ** //
      liveRecu.fromJSON(liveRecuJson);

      // ** changer d'activity avec le live créé en parametre ** //
      Intent i = new Intent(CreerLiveActivity.this, SuivreLiveActivity.class);
      i.putExtra("Type", "LiveCreation");
      // TODO : refaire des test de passage de Live sans le parametre mais en global
      SuivreLiveActivity.gLiveASuivre = liveRecu;
      // i.putExtra("LiveCreation", liveRecu);
      startActivity(i);

    }
  }

}
